package com.hx.er.action.customer.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErCustomerAction;
import com.hx.er.bean.base.LanguageBean;
import com.hx.er.bean.base.UserBean;
import com.hx.er.common.logic.SessionUtil;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.util.CharUtil;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.customer.ICustomerInfoService;
import com.opensymphony.xwork2.ActionContext;

public class CustomerInfoAction extends ErCustomerAction {

	// 序列化ID
	private static final long serialVersionUID = 8547444991500774322L;

	private UserBean userBeanPage;
	private List<Integer> ageList;
	private List<LanguageBean> langList;
	private Map<String, String> sexMap;

	@Autowired
	private ICustomerInfoService customerInfoService;

	public String init() {
		// 初始化常量
		this.initInputPage();

		// 获取用户编号
		long userId = super.userInfoLogin.getUserId();
		// 获取用户信息
		this.userBeanPage = this.customerInfoService.getUserAliveById(userId);
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			this.initInputPage();
			return INPUT;
		}
		
		// 设定用户类型
		this.userBeanPage.setUserTypeId(Constants.USER_TYPE_CUSTOMER);

		// 更新用户数据
		int count = this.customerInfoService.updateUserInfo(this.userBeanPage);

		if (count == 0) {
			// 显示错误信息
			this.addActionError("更新失败！");
			// 初始化页面
			this.initInputPage();
			// 页面跳转
			return INPUT;
		} else {
			// 更新session中的用户信息
			UserBean newUserBean = this.customerInfoService.getUserAliveById(this.userBeanPage.getUserId());
			SessionUtil.setAttributes(ActionContext.getContext(), Constants.SESSION_USER_INFO, newUserBean);
			// 显示成功信息
			this.addActionMessage("更新成功！");
			// 页面跳转
			return SAVE;
		}
	}

	public String back() {
		return BACK;
	}

	/**
	 * 初始化页面需要的下拉框等元素
	 */
	private void initInputPage() {
		// 初始化年龄，范围为1～100
		this.ageList = new ArrayList<Integer>();
		for (int i = 10; i < 100; i++) {
			this.ageList.add(i + 1);
		}

		// 初始化语言
		this.langList = ErLanguages.getLangList();

		// 初始化性别
		this.sexMap = new HashMap<String, String>();
		this.sexMap.put("0", "女");
		this.sexMap.put("1", "男");
	}

	/**
	 * 页面验证
	 */
	private boolean checkParams() {
		// 用户名验证
		if (StringUtil.isEmpty(this.userBeanPage.getUserName())) {
			this.addFieldError("msg.userName", "用户名不能为空!");
		} else if (!StringUtil.isHalf(this.userBeanPage.getUserName())) {
			this.addFieldError("msg.userName", "用户名只能是半角字符!");
		} else if (!StringUtil.checkLength(this.userBeanPage.getUserName(), 6, 16)) {
			this.addFieldError("msg.userName", "用户名长度只能在6-16位之间!");
		} else if (!CharUtil.isLetter(this.userBeanPage.getUserName().charAt(0))) {
			this.addFieldError("msg.userName", "用户名首位必须是字母");
		}

		// 昵称验证
		if (!StringUtil.isEmpty(this.userBeanPage.getNickName())) {
			if (!StringUtil.checkLength(this.userBeanPage.getNickName(), 0, 64)) {
				this.addFieldError("msg.nickName", "昵称长度不能超过64字节(32个汉字)!");
			}
		}

		// 邮箱验证
		if (!StringUtil.isEmpty(this.userBeanPage.getMail())) {
			if (!StringUtil.isHalf(this.userBeanPage.getMail())) {
				this.addFieldError("msg.mail", "邮箱地址只能是半角字符!");
			} else if (!StringUtil.checkLength(this.userBeanPage.getMail(), 0, 100)) {
				this.addFieldError("msg.mail", "邮箱地址长度不能超过100字节!");
			} else if (!StringUtil.isEmailAddr(this.userBeanPage.getMail())) {
				this.addFieldError("msg.mail", "邮件地址格式错误!");
			}
		}

		// 手机号码验证
		if (!StringUtil.isEmpty(this.userBeanPage.getMobile())) {
			if (!StringUtil.checkLength(this.userBeanPage.getMobile(), 0, 15)) {
				this.addFieldError("msg.mobile", "手机号长度不能超过15字节!");
			} else if (!StringUtil.isNumeric(this.userBeanPage.getMobile())) {
				this.addFieldError("msg.mobile", "手机号格式错误!");
			}
		}

		// 地址验证
		if (!StringUtil.isEmpty(this.userBeanPage.getAddress())) {
			if (!StringUtil.checkLength(this.userBeanPage.getAddress(), 0, 100)) {
				this.addFieldError("msg.address", "地址长度不能超过100字节!");
			}
		}

		// 描述验证
		if (!StringUtil.isEmpty(this.userBeanPage.getDescription())) {
			if (!StringUtil.checkLength(this.userBeanPage.getDescription(), 0, 255)) {
				this.addFieldError("msg.description", "描述长度不能超过255字节!");
			}
		}

		// 备注验证
		if (!StringUtil.isEmpty(this.userBeanPage.getRemark())) {
			if (!StringUtil.checkLength(this.userBeanPage.getRemark(), 0, 255)) {
				this.addFieldError("msg.remark", "备注长度不能超过255字节!");
			}
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public UserBean getUserBeanPage() {
		return userBeanPage;
	}

	public List<Integer> getAgeList() {
		return ageList;
	}

	public void setAgeList(List<Integer> ageList) {
		this.ageList = ageList;
	}

	public List<LanguageBean> getLangList() {
		return langList;
	}

	public void setLangList(List<LanguageBean> langList) {
		this.langList = langList;
	}

	public Map<String, String> getSexMap() {
		return sexMap;
	}

	public void setSexMap(Map<String, String> sexMap) {
		this.sexMap = sexMap;
	}

	public void setUserBeanPage(UserBean userBeanPage) {
		this.userBeanPage = userBeanPage;
	}

}
